package com.UtilsPak;

import javax.faces.model.SelectItem;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;


/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 18-Mar-2011
 * Time: 3:26:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class DateTimeFunctions {


    public static Date getDate(String value) {
        Date retVal = null;

        if (value != null){
            try{
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                retVal = df.parse(value);
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }

    public static String dayAdd (Date value, int dayadd) {
        String retVal = null;

        if (value != null){
            try{
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Calendar cal= Calendar.getInstance();
                cal.setTime(value);
                cal.add(Calendar.DAY_OF_YEAR,dayadd);
                retVal =df.format(cal.getTime());
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }

    public static String getDayOfWeek (String value, Locale l ) {
        String retVal = "";

        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance(l);
                cal.setTime(d);
                String[] weekdays = new DateFormatSymbols(l).getShortWeekdays();
                retVal = weekdays[cal.get(Calendar.DAY_OF_WEEK) ] ;
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }

    public static String getDayOfMonth (String value ) {
        String retVal = "";

        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance();
                cal.setTime(d);
                retVal = Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }


    public static String getMonth (String value, Locale l ) {
        String retVal = "";

        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance(l);
                cal.setTime(d);
                String[] months = new DateFormatSymbols(l).getMonths();
                retVal = months[cal.get(Calendar.MONTH)];
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }


     public static String getYear (String value, Locale l ) {
        String retVal = "";

        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance(l);
                cal.setTime(d);
                retVal = Integer.toString(cal.get(Calendar.YEAR));
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }


     public static String getDaysInMonth (String value) {
        String retVal = "";

        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance();
                cal.setTime(d);
                retVal = Integer.toString(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                retVal=null;
                return retVal;
            }

        }
        return  retVal;
    }

    public static String isWeekEnd(String value) {
        if (value != null){
            try{
                Date d = getDate(value);
                Calendar cal= Calendar.getInstance();
                cal.setTime(d);
                if(Calendar.SUNDAY == cal.get(Calendar.DAY_OF_WEEK) || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                    return "true";
                } else {
                    return "false";
                }
            } catch(Exception e){
                System.err.println("Exception: " + e.getMessage());
                return "false";
            }
        }
        return  "false";
    }


}
